package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @program: Spring_day03_04_综合案例
 * @description:
 * @author: zhanghz001
 * @create: 2020-11-29 17:05
 **/
@Component
@Aspect
public class RunTimeMonitorAdvice {

    @Around("AOPPointcut.pt()")
    public Object aop005around(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        Signature signature = pjp.getSignature();
        System.out.println(signature);
        System.out.println(signature.getDeclaringTypeName());
        System.out.println(signature.getName());
        System.out.println();
        Object ret = pjp.proceed();
        System.out.println("接口耗时: "+(System.currentTimeMillis()-start));
        return ret;
    }
    public Object runtimeAround(ProceedingJoinPoint pjp) throws Throwable {
        //获取执行签名信息
        Signature signature = pjp.getSignature();
        //通过签名获取执行类型（接口名）
        String className = signature.getDeclaringTypeName();
        //通过签名获取执行操作名称（方法名）
        String methodName = signature.getName();

        //执行时长累计值
        long sum = 0L;

        for (int i = 0; i < 10000; i++) {
            //获取操作前系统时间beginTime
            long startTime = System.currentTimeMillis();
            //原始操作调用
            pjp.proceed(pjp.getArgs());
            //获取操作后系统时间endTime
            long endTime = System.currentTimeMillis();
            sum += endTime-startTime;
        }
        //打印信息
        System.out.println(className+":"+methodName+"   (万次）run:"+sum+"ms");
        return null;
    }
}
